<template>
  <div class="md-con">
    <div :contenteditable="contenteditable" v-html="compiledMarkdown($attrs.content)"></div>
  </div>
</template>

<script>
import marked from "marked";
export default {
  name: "hkMd",
  props: {
    contenteditable: {
      type: Boolean,
      default: false,
    },
  },
  components: {},
  data() {
    return {};
  },
  mounted() {},
  methods: {
    compiledMarkdown(text) {
      // console.log(text, "text")
      let that = this;
      let rendererMD = new marked.Renderer();
      rendererMD.link = function (href, title, text) {
        return `<a href="${href}" title="${text}" target="_blank"> ${text}</a>`;
      };
      // 自定义标题渲染方法
      rendererMD.heading = function (text, level) {
        // console.log("[ text ] >", text);
        // console.log("[ level ] >", level);
        // const escapedText = text.toLowerCase().replace(/^\s+|\s+$/g, "");
        // console.log("escapedText", escapedText);
        // let id = that.utils.uuid();
        // that.$bus.$emit("MDTitleCreate", { text, level, id });
        return `<h${level} id="${text}">${text}</h${level}>\n`;
      };
      return text ? marked(text.trim(), { gfm: true, tables: true, renderer: rendererMD }) : "";
    },
  },
};
</script>
<style scoped lang="scss">
.md-con {
  line-height: 1.6;
  background: var(--answerBg);
  letter-spacing: 1px;
  width: 100%;
  position: relative;
  overflow-x: auto;
  color: #1F2733;
  // font-family: NotoSans;
  ::v-deep p {
    margin-bottom: 10px;
    img {
      max-width: 100%;
    }
  }
  ::v-deep h3 {
    margin-bottom: 12px;
  }
   ::v-deep h4 {
    margin-bottom: 12px;
  }
  ::v-deep ol {
    margin: 10px 0;
    padding-left: 20px;
    li {
      list-style: auto;
    }
    li::marker {
      font-weight: bold; /* 设置序号加粗 */
    }
  }
  ::v-deep li {
    font-size: 14px;
    line-height: 19px;
    margin-bottom: 8px;
  }
  ::v-deep ul {
    margin: 10px 0 16px;
    padding-left: 28px;
    li {
      list-style-type: decimal;
    }
    li::marker {
      font-weight: bold; /* 设置序号加粗 */
    }
  }
  ::v-deep ol {
    ul {
      padding-left: 15px;
      li {
        list-style-type: disc !important;
      }
    }
  }
  ::v-deep ul {
    ul {
      padding-left: 15px;
      li {
        list-style-type: disc !important; // 序号为圆点
      }
    }
  }
  ::v-deep a {
    color: $themeColor;
  }
  .element {
    -ms-overflow-style: none; /* IE 和 Edge */
    scrollbar-width: none; /* Firefox */
  }
  ::v-deep pre {
    overflow-x: auto;
    margin-bottom: 12px;
  }
  ::v-deep textarea {
    width: 100%;
  }
  ::v-deep textarea:focus-visible {
    border: none;
  }
  ::v-deep code {
    white-space: break-spaces;
    margin-bottom: 12px;
  }
  ::v-deep table {
    width: 100%;
    border-collapse: collapse; /* 去掉单元格之间的距离 */
    margin: 12px 0px;
    th,
    td {
      border: 1px solid #ddd; /* 设置单元格边框  #e8e8eb */
      padding: 8px; /* 设置单元格内边距 */
      text-align: left; /* 设置文本对齐方式 */
    }
  }
}
</style>
